package edu.metrostate.capstonediner;

import android.os.Bundle;
import android.os.Looper;
import android.app.Activity;
import android.app.AlertDialog;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.support.v4.app.NavUtils;
import android.annotation.TargetApi;
import android.content.DialogInterface;
import android.os.Build;

public class CreateAccountActivity extends Activity implements Runnable {
	
	private CreateAccountActivity myInstance;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_create_account);
		// Show the Up button in the action bar.
		setupActionBar();
		
        myInstance = this;
        
        // Buttons
        Button btnCreate = (Button) findViewById(R.id.CreateAccountButton);
        
        // Login with the username and password
        btnCreate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            	new Thread(myInstance).start();
            }
        });
	}

	/**
	 * Set up the {@link android.app.ActionBar}, if the API is available.
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setupActionBar() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.create_account, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void run() {
		String user = ((EditText)findViewById(R.id.usernameText)).getText().toString();
    	String password = ((EditText)findViewById(R.id.passwordText)).getText().toString();
    	String password2 = ((EditText)findViewById(R.id.password2Text)).getText().toString();
    	String firstName = ((EditText)findViewById(R.id.firstNameText)).getText().toString();
    	String lastName = ((EditText)findViewById(R.id.lastNameText)).getText().toString();
    	
    	Looper.prepare();
    	AlertDialog.Builder builder = new AlertDialog.Builder(myInstance);
    	
    	if (user. length() > 0 && password.length() > 0 && firstName.length() > 0 && lastName.length() > 0) {
	        if (password.equals(password2)) {
		    	if (DatabaseHTTP.SaveUser(user, password, firstName, lastName)) {
		    		GameActivity.setUser(user);
		    		builder.setMessage("Congratulations!  Your account has been successfully created and you are now logged in.");
		    		builder.setPositiveButton("Okay!", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							setResult(RESULT_OK, null);
				    	    finish();
						}
					});
		            builder.create().show();
		            Looper.loop();
		    	} else {
		            builder.setMessage("Failed to create user account please try again with a different username.");
		            builder.create().show();
		            Looper.loop();
		    	}
	        } else {
	            builder.setMessage("Password does not match.");
	            builder.create().show();
	            Looper.loop();
	        }
    	} else {
    		builder.setMessage("Please make sure that you fill in all of the fields.");
            builder.create().show();
            Looper.loop();
    	}
	}

}
